Completed
Push — master ( ac7736...25cc0e )
by Andres
27s
created

angular.controller(ꞌmain-loopꞌ)   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
nc 4
dl 0
loc 21
rs 9.0534
nop 0
1
'use strict';
2
3
angular
4
  .module('game')
5
  .controller('main-loop', ['$scope',
6
    '$interval',
7
    '$timeout',
8
    'savegame',
9
    'state',
10
    'data',
11
    'util',
12
    'reaction',
13
    function($scope, $interval, $timeout, savegame, state, data, util, reaction) {
14
      $scope.state = state;
15
      $scope.data = data;
16
      $scope.util = util;
17
18
      let self = this;
19
20
      self.update = function() {
21
        state.reactions = [];
22
        state.update(state.player);
23
        reaction.processReactions(state.reactions, state.player);
24
      };
25
26
      self.updateLoop = function() {
27
        self.update();
28
        let speed = 1000;
29
        if(state.fasterTicks){
30
          speed = 1;
31
          state.player.offline--;
32
          if(state.player.offline <= 0) state.fasterTicks = 0;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
33
        }
34
        $timeout(self.updateLoop, speed);
35
      };
36
37
      self.startup = function() {
38
        savegame.load();
39
        let elapsed = Math.floor(Date.now()/1000)-state.player.last_login;
40
        let total = util.calculateValue(data.global_upgrades.offline_time.power.base,
41
            data.global_upgrades.offline_time.power,
42
            state.player.global_upgrades.offline_time);
43
        // lets limit the offline elapsed time
44
        state.player.offline = Math.min(total, state.player.offline+elapsed);
45
46
        state.loading = false;
47
        // trigger the game loop
48
        $timeout(self.updateLoop, 1000);
49
        $interval(savegame.save, 10000);
50
      };
51
52
      $timeout(self.startup);
53
    }
54
  ]);
55